// 示例: app/src/main/java/com/example/managemyself/data/model/Task.java
package com.example.managemyself.data.model;

import androidx.room.Entity;
import androidx.room.PrimaryKey;
import androidx.room.TypeConverters;

import com.example.managemyself.util.DateConverter;

import java.util.Date;

@Entity(tableName = "tasks") // Room 注解
public class Task {
    @PrimaryKey(autoGenerate = true)
    private int id;

    private String title;
    private String description;
    private String status; // 例如: "待完成", "已完成"
    private int importance; // 重要程度
    private String urgency; // 例如: "紧急", "一般"
    private String repeat; // 例如: "每天", "每周"
    private String type;    // 类型：学习、锻炼、养生
    private Date dueDate;  // 截止日期
    private Date createdDate; // 创建日期
    private Date completedDate; // 完成日期
    private boolean isCompleted; // 是否已完成

    // 构造函数
    public Task(String title, String description) {
        this.title = title;
        this.description = description;
        this.status = "待完成";
        this.createdDate = new Date();
        this.isCompleted = false;
        this.importance = 0; // 默认重要程度
    }

    // Getters and Setters
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public int getImportance() {
        return importance;
    }

    public void setImportance(int importance) {
        this.importance = importance;
    }

    public String getUrgency() {
        return urgency;
    }

    public void setUrgency(String urgency) {
        this.urgency = urgency;
    }

    public String getRepeat() {
        return repeat;
    }

    public void setRepeat(String repeat) {
        this.repeat = repeat;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Date getDueDate() {
        return dueDate;
    }

    public void setDueDate(Date dueDate) {
        this.dueDate = dueDate;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public Date getCompletedDate() {
        return completedDate;
    }

    public void setCompletedDate(Date completedDate) {
        this.completedDate = completedDate;
    }

    public boolean isCompleted() {
        return isCompleted;
    }

    public void setCompleted(boolean completed) {
        isCompleted = completed;
        if (completed) {
            this.status = "已完成";
            this.completedDate = new Date(); // 记录完成时间
        } else {
            this.status = "待完成";
            this.completedDate = null; // 清除完成时间
        }
    }

    /**
     * 将紧急程度文本转换为可排序的数值
     * 数值越小，代表越紧急
     * @return int 紧急程度的数值表示
     */
    public int getUrgencyValue() {
        if (this.urgency == null || this.urgency.isEmpty()) {
            return 5; // 默认值
        }
        
        // 如果urgency是数字字符串（来自滑块），直接转换
        try {
            int numericUrgency = Integer.parseInt(this.urgency);
            // 滑块值是0-10，转换为1-10的紧急程度（10最紧急，1最不紧急）
            return 11 - numericUrgency; // 反转数值，使得10变成1（最紧急）
        } catch (NumberFormatException e) {
            // 如果不是数字，按原来的文本处理
            switch (this.urgency) {
                case "非常紧急":
                    return 1;
                case "紧急":
                    return 2;
                case "一般":
                    return 3;
                case "不紧急":
                    return 4;
                default:
                    return 5; // 未指定或未知的值排在最后
            }
        }
    }
    
    /**
     * 获取紧急程度的显示文本
     * @return String 紧急程度的文本表示
     */
    public String getUrgencyDisplayText() {
        if (this.urgency == null || this.urgency.isEmpty()) {
            return "一般";
        }
        
        // 如果urgency是数字字符串，转换为文本描述
        try {
            int numericUrgency = Integer.parseInt(this.urgency);
            if (numericUrgency >= 8) {
                return "非常紧急 (" + numericUrgency + ")";
            } else if (numericUrgency >= 6) {
                return "紧急 (" + numericUrgency + ")";
            } else if (numericUrgency >= 4) {
                return "一般 (" + numericUrgency + ")";
            } else {
                return "不紧急 (" + numericUrgency + ")";
            }
        } catch (NumberFormatException e) {
            // 如果不是数字，直接返回原文本
            return this.urgency;
        }
    }
}